package com.xfcycle.zebrabikegp.map;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationManager;
import android.support.v4.app.ActivityCompat;
import android.util.Log;

import java.util.List;

/**
 * 自定义位置工具类
 * Created by Wilson on 2017/7/6.
 */

public class CustomLocationUtil {

    /**
     * 获取当前所在的经纬度信息，第一个为经度longitude 第二个为纬度 latitude
     */
    public static Location obtainCurrentLocation(Context context) {

        Location location = null;
        String locationProvider;

        //获取地理位置管理器
        LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        //获取所有可用的位置提供器
        List<String> providers = locationManager.getProviders(true);
        if (providers.contains(LocationManager.GPS_PROVIDER)) {
            //如果是GPS
            locationProvider = LocationManager.GPS_PROVIDER;
        } else if (providers.contains(LocationManager.NETWORK_PROVIDER)) {
            //如果是Network
            locationProvider = LocationManager.NETWORK_PROVIDER;
        } else {
            Log.e("CustomLocationUtil", "obtainCurrentLocation:  没有可用的位置提供器");
            return null;
        }
        //获取Location 首先需要权限判断
        if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
                ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            return null;
        }
        location = locationManager.getLastKnownLocation(locationProvider);
        return location;
    }
}
